package com.jbinfo.qingcaowan.interceptor;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.jbinfo.qingcaowan.common.SessionSecurityConstants;
import com.jbinfo.qingcaowan.security.common.IpUtil;
import com.jbinfo.qingcaowan.security.common.LoginContext;

import io.terminus.pampas.common.BaseUser;
import io.terminus.pampas.common.UserUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 从session中读取用户信息，设置到线程上线文中
 * 
 * @author hanty
 * 
 */
@Slf4j
public class LoginContextInterceptor extends HandlerInterceptorAdapter {

	private final static String DEFAULT_LOGIN = "redirectLogin"; 

	/**
	 * 用户session中的用户表示
	 */
	private String keyUserName = SessionSecurityConstants.KEY_USER_NAME;
	private String keyUserNickName = SessionSecurityConstants.KEY_USER_NICK_NAME;
	private String keyUserId = SessionSecurityConstants.KEY_USER_ID;
	private String keyLocalLanguage = SessionSecurityConstants.KEY_LOCAL_LANGUAGE;

	public void setKeyUserName(String keyUserName) {
		this.keyUserName = keyUserName;
	}

	public void setKeyUserId(String keyUserId) {
		this.keyUserId = keyUserId;
	}

	public void setKeyLocalLanguage(String keyLocalLanguage) {
		this.keyLocalLanguage = keyLocalLanguage;
	}
	/**
	 * 预处理
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//HttpSession httpSession = request.getSession();
		//初始化登录用户
		//initlizeBaseUser(httpSession);
		//LoginContext loginContext = buildLoginContext(request);
		//LoginContextHolder.put(loginContext);
		return super.preHandle(request, response, handler);
	}

	/**
	 * 拦截
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
//		if (!isLogin(request)) {
//			redirectToLogin(request, response);
//		}
//		LoginContext loginContext = buildLoginContext(request);
//		LoginContextHolder.put(loginContext);
		//LoginContextHolder.clear();
		//UserUtil.clearCurrentUser();
		super.postHandle(request, response, handler, modelAndView);
//		LoginContextHolder.clear();
	}

	/**
	 * 返回方法拦截
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		if (ex != null) {
//			log.debug(ex.getMessage());
		}
		super.afterCompletion(request, response, handler, ex);
	}

	/**
	 * 判断是否登录
	 * @param httpServletRequest
	 * @return
	 */
	protected boolean isLogin(HttpServletRequest httpServletRequest) {
		HttpSession httpSession = httpServletRequest.getSession();
		String userName = (String) httpSession.getAttribute(keyUserName);
		if (userName == null) {
			// 仅仅记住get请求的链接
			if (StringUtils.equalsIgnoreCase(httpServletRequest.getMethod(),
					"GET")) {
				HttpSession session = httpServletRequest.getSession();
				String servletPath = httpServletRequest.getServletPath();
				String fullURL = new StringBuffer(servletPath).append(
						toParameterString(httpServletRequest)).toString();
				session.setAttribute(
						SessionSecurityConstants.KEY_LAST_VISIT_URL, fullURL);
			}
			return false;
		}
		return true;
	}

	/**
	 * 封装登录用户信息到用户登录上下文中，并将登录用户封装到HOP4.0用BaseUser中
	 * @param httpServletRequest
	 * @return
	 */
	protected LoginContext buildLoginContext(
			HttpServletRequest httpServletRequest) {
		HttpSession httpSession = httpServletRequest.getSession();
		String userName = (String) httpSession.getAttribute(keyUserName);
		Long userId = (Long) httpSession.getAttribute(keyUserId);
		Locale language = (Locale) httpSession.getAttribute(keyLocalLanguage);
		LoginContext loginContext = createLoginContext();
		loginContext.setUserId(userId);
		loginContext.setUserName(userName);
		loginContext.setIp(IpUtil.getIpAddress(httpServletRequest));
		loginContext.setLanguage(language == null ? "zh_CN" : language.toString());
		return loginContext;
	}

	/**
	 * 创建用户登录上下文
	 * @return
	 */
	protected LoginContext createLoginContext() {
		return new LoginContext();
	}

	/**
	 * 
	 * @param httpServletRequest
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private String toParameterString(HttpServletRequest httpServletRequest) {
		Enumeration<String> paramEnumeration = httpServletRequest.getParameterNames();
		if (!paramEnumeration.hasMoreElements()) {
			return "";
		}
		StringBuffer stringBuffer = new StringBuffer();
		while (paramEnumeration.hasMoreElements()) {
			String paramName = paramEnumeration.nextElement();
			stringBuffer.append("&");
			stringBuffer.append(paramName);
			stringBuffer.append("=");
			stringBuffer.append(httpServletRequest.getParameter(paramName));
		}
		stringBuffer.replace(0, 1, "?");
		return stringBuffer.toString();
	}

	/**
	 * 重定向到登录页面
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	protected void redirectToLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.sendRedirect(DEFAULT_LOGIN);
	}
	
	/**
	 * 初始化HOP4.0用登录用户信息
	 * @param userID
	 */
	protected void initlizeBaseUser(HttpSession httpSession){
		// HOP4.0添加用户到BASE_USER
		BaseUser user = new BaseUser();
		user.setId((Long)httpSession.getAttribute(keyUserId));
		user.setName((String)httpSession.getAttribute(keyUserName));
		user.setNickName((String)httpSession.getAttribute(keyUserNickName));
		UserUtil.putCurrentUser(user);
	}
	
	public static void main(String[] args) {
		BaseUser user = new BaseUser();
		user.setId(1L);
		user.setName("234");
		user.setNickName("234");
		BaseUser user1 = new BaseUser();
		user1.setId(2L);
		user1.setName("123");
		user1.setNickName("123");
		UserUtil.putCurrentUser(user);
		UserUtil.putCurrentUser(user1);
		System.out.println(UserUtil.getCurrentUser());
		System.out.println(UserUtil.getMasterId());
		UserUtil.clearCurrentUser();
		UserUtil.getCurrentUser();
	}
}
